package com.usian.project.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.usian.commom.pojo.Area;
import com.usian.commom.pojo.Project;
import com.usian.project.fegin.TypeFegin;
import com.usian.project.mapper.AreaMapper;
import com.usian.project.mapper.ProjectMapper;

import com.usian.project.service.ProjectService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.usian.commom.pojo.Type;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @TODO:
 * @Author:baimu
 * @Date:2024 10 28 14 22
 **/

@Service
@Slf4j
public class ProjectServiceImpl implements ProjectService {
    final
    ProjectMapper projectMapper;


    @Autowired
    AreaMapper areaMapper;

    @Autowired TypeFegin typeFegin;

    public ProjectServiceImpl(ProjectMapper projectMapper) {
        this.projectMapper = projectMapper;
    }

    @Override
    public Integer insertProject(Project project) {
        if(project.getId()!=null){
            return projectMapper.updateProject(project);

        }

        return projectMapper.insertProject(project);
    }

    @Override
    public Integer deleteProjectById(Integer id) {
        return projectMapper.deleteProjectById(id);
    }

    @Override
    public PageInfo<Project> findAll(Integer pageNo, Integer pageSize,Project project1) {
        //开启分页查询,当执行查询时，插件进行相关的sql拦截进行分页操作，返回一个page对象
         PageHelper.startPage(pageNo, pageSize);

        List<Project> projectList = projectMapper.findAll(project1);


        List<Type> typeList = typeFegin.findAll();

        // map  key-value ;通过key去取value
        Map<Integer, Type> typeMap =  new HashMap<Integer, Type>();
        typeList.forEach(type->{
            typeMap.put(type.getId(),type);
        });

        projectList.forEach(project->{
            project.setTypeName(typeMap.get(project.getTypeId()).getName());
        });

        PageInfo<Project> projectPageInfo = new PageInfo<>(projectList);
        log.info("projectPageInfo:{}",projectPageInfo);
        log.info("projectPageInfo:{}",projectPageInfo.toString());
        return projectPageInfo;
    }

    @Override
    public List<Area> findAllArea() {
        List<Area> allAreaList = areaMapper.findAll();

        List<Area> oneAreaList =  new ArrayList<Area>();
        //先找一级
        allAreaList.forEach(area -> {
            if(area.getPid().intValue() ==0){
                buildChildren(area,allAreaList);
                oneAreaList.add(area);
            }
        });


        return oneAreaList;
    }


    /**
     * 从所有的数据里面找下一级
     */
    public  void  buildChildren (Area area,List<Area> allAreaList){

        List<Area> childrenAreaList = new ArrayList<Area>();


        allAreaList.forEach(area1 -> {
            Integer pid = area1.getPid();
            Integer id = area.getId();
            if(pid.intValue()==id.intValue()){
                buildChildren(area1,allAreaList);
                childrenAreaList.add(area1);
            }

        });
        if(childrenAreaList.size()>0){
            area.setChildren(childrenAreaList);
        }else{
            area.setChildren(null);
        }


    }
}
